import store from '../../westore/store'
import create from '../../westore/utils/create'

import Config from "../../config"
import Api from "../../utils/api"
import {
  Modal
} from "../../utils/common"
import deepCopy from "../../utils/common"
const app = getApp(),
  regeneratorRuntime = require("../../utils/regenerator-runtime/runtime")

create(store, {
  /**
   * 页面的初始数据
   */
  data: {
    openid: "",
    mypagenum: 1,
    myActivityData: [],
    mypages:1,
    current:"my",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    wx.showLoading({
      title: '加载中',
    })
    const _this = this;
    const openid = _this.store.data.openid;
    const pagenum = 1;
    const ismy = _this.data.current == "my"
    const type = _this.data.current
    const res = ismy ? await Api.getActivityePage(pagenum, openid) : await Api.getmyActivityePage(type, openid, pagenum);
    wx.hideLoading();
    if (res.status == 0) {
      _this.setData({
        myActivityData: res.data.items
      })
    }
  },

  goAddActivity: function (e) {
    const thetype = e.currentTarget.dataset.thetype
    const actid = e.currentTarget.dataset.actid
    // console.log(e.currentTarget.dataset)
    wx.navigateTo({
      url: '/pages/addActivity/addActivity?thetype=' + thetype + '&theactid=' + actid
    })
  },
  handleChange: async function ({ detail }) {
    wx.showLoading({
      title: '请稍侯...',
    })
    this.setData({
      current: detail.key
    });
    const _this = this;
    const openid = _this.store.data.openid;
    const ismy = _this.data.current == "my"
    let pagenum =  1
    const type = _this.data.current
    const res = ismy ? await Api.getActivityePage(pagenum, openid) : await Api.getmyActivityePage(type, openid, pagenum);
    wx.hideLoading();
    if (res.status == 0) {
      _this.setData({
        myActivityData: res.data.items,
        mypagenum: pagenum
      })
    } else {
      wx.showModal({
        title: '错误',
        content: '网络异常，请稍后重试',
        showCancel: false,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: async function () {
    const _this = this;
    const openid = _this.store.data.openid;
    const ismy = _this.data.current == "my"
    let pagenum = _this.data.mypagenum + 1
    const pages = _this.data.mypages 
    const type = _this.data.current
    if (pagenum > pages) return;
    wx.showLoading({
      title: '请稍侯...',
    })
    const res = ismy ? await Api.getActivityePage(pagenum, openid) : await Api.getmyActivityePage(type, pagenum, openid);
    wx.hideLoading();
    if (res.status == 0) {
      _this.setData({
        myActivityData: _this.data.myActivityData.concat(res.data.items),
        mypagenum: pagenum
      })
    } else {
      wx.showModal({
        title: '错误',
        content: '网络异常，请稍后重试',
        showCancel: false,
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})